home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / FlushHeaders / new-source / NewSource.lha / FlushHeaders.c < prev    next >
C/C++ Source or Header  |  2001-06-10  |  9KB  |  420 lines

  1. /*
  2. ** $Log$
  3. */
  4.  
  5. /*
  6. **   YAM2NN - Usenet access for YAM p7 and newer
  7. **   Copyright (C) 1999 Karol Bryd <kbryd@femina.com.pl>
  8. **
  9. **   This program is free software; you can redistribute it and/or
  10. **   modify it under the terms of the GNU General Public License
  11. **   as published by the Free Software Foundation; either version 2
  12. **   of the License, or (at your option) any later version.
  13. **
  14. **   This program is distributed in the hope that it will be useful,
  15. **   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. **   GNU General Public License for more details.
  18. **
  19. **   You should have received a copy of the GNU General Public License
  20. **   along with this program; if not, write to the Free Software
  21. **   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28.  
  29. #include <proto/dos.h>
  30. #include <proto/exec.h>
  31. #include <proto/utility.h>
  32. #include <proto/rexxsyslib.h>
  33.  
  34. #include <rexx/rxslib.h>
  35. #include <rexx/rexxio.h>
  36. #include <rexx/errors.h>
  37.  
  38. #include <dos/dos.h>
  39. #include <exec/execbase.h>
  40. #include <exec/memory.h>
  41. #include <utility/tagitem.h>
  42.  
  43. #include "get_folder_path.h"
  44.  
  45. __stkargs void NewList(struct List * list);
  46.  
  47. struct NewsNode {
  48.     struct NewsNode * next;
  49.     struct NewsNode * prev;
  50.     char * ngname;
  51.     char * path;
  52.     char folder[40];
  53. };
  54.  
  55. struct List * newsgroupslist = 0;
  56. struct List * folders = 0;
  57. APTR listpool = 0;
  58. char buf[1000];
  59.  
  60. char * stristr(char * buf, char * str)
  61. {
  62.     register int len = strlen(buf);
  63.     register int len2 = strlen(str);
  64.     register int a;
  65.  
  66.     for(a = 0; a <= len - len2; a++)
  67.         if(Strnicmp(&buf[a], str, len2) == 0)
  68.             return(&buf[a]);
  69.     return(0);
  70. }
  71.  
  72. int LoadConfig(void)
  73. {
  74.     BPTR fh;
  75.     char * tmp;
  76.  
  77.     if(fh = Open("YAM:Yam2NN.config", MODE_OLDFILE))
  78.     {
  79.         while(FGets(fh, buf, 200))
  80.         {
  81.             if(strlen(buf) < 2)
  82.                 break;
  83.             if(buf[0] == ';')
  84.                 continue;
  85.             if(!(Strnicmp(buf, "Newsgroups:", 11)))
  86.             {
  87.                 struct NewsNode * newsnode;
  88.  
  89.                 if(!newsgroupslist)
  90.                 {
  91.                     if(newsgroupslist = AllocPooled(listpool, sizeof(struct List)))
  92.                         NewList(newsgroupslist);
  93.                     else
  94.                         return(FALSE);
  95.                 }
  96.                 if(newsnode = AllocPooled(listpool, sizeof(struct NewsNode)))
  97.                 {
  98.                     tmp = strtok(buf + 12, " ");
  99.                     if(newsnode->ngname = AllocPooled(listpool, strlen(tmp) + 1))
  100.                     {
  101.                         strcpy(newsnode->ngname, tmp);
  102.                         tmp = strtok(NULL, " \n");
  103.                         if(!tmp)
  104.                             return(FALSE);
  105.                         if(newsnode->path = AllocPooled(listpool, strlen(tmp) + 1))
  106.                         {
  107.                             strcpy(newsnode->path, tmp);
  108.                             {
  109.                                 int a;
  110.  
  111.                                 a = strlen(tmp);
  112.                                 if(tmp[a - 1] == '/')
  113.                                     tmp[a - 1] = 0;
  114.                                 while(tmp[a] != '/' && tmp[a] != ':' && a > 0)
  115.                                     a--;
  116.                                 strcpy(newsnode->folder, &tmp[a + 1]);
  117.                             }
  118.                             while(1)
  119.                             {
  120.                                 if(!(FGets(fh, buf, 200)))
  121.                                     break;
  122.                                 if(Strnicmp(buf, "\n", 1) == 0)
  123.                                     break;
  124.                             }
  125.                             AddTail(newsgroupslist, (struct Node *)newsnode);
  126.                         }
  127.                         else
  128.                             return(FALSE);
  129.                     }
  130.                     else
  131.                         return(FALSE);
  132.                 }
  133.                 else
  134.                     return(FALSE);
  135.             }
  136.         }
  137.         Close(fh);
  138.         return(TRUE);
  139.     }
  140.     else
  141.         return(FALSE);
  142. }
  143.  
  144. // this function was written by Christian Hattemer <Chris@heaven.riednet.wh.tu-darmstadt.de>
  145.  
  146. long SendRexxCommand(char * Port, char * Cmd, struct MsgPort * ReplyPort, char * Result)
  147. {
  148.     ULONG Error;
  149.     struct MsgPort * RexxPort;
  150.  
  151.     Forbid();
  152.     if(RexxPort = FindPort(Port))
  153.     {
  154.         struct RexxMsg * rexxMsg, * Answer;
  155.  
  156.         if(rexxMsg = CreateRexxMsg(ReplyPort, NULL, NULL))
  157.         {
  158.             if(rexxMsg->rm_Args[0] = CreateArgstring(Cmd, strlen(Cmd)))
  159.             {
  160.                 rexxMsg->rm_Action = RXCOMM | RXFF_RESULT;
  161.                 PutMsg(RexxPort, &rexxMsg->rm_Node);
  162.                 do
  163.                 {
  164.                     WaitPort(ReplyPort);
  165.                     Answer = (struct RexxMsg *)GetMsg(ReplyPort);
  166.                 } while (Answer == NULL);
  167.                 Permit();
  168.                 if((Error = Answer->rm_Result1) == RC_OK)
  169.                 {
  170.                     if(Answer->rm_Result2)
  171.                     {
  172.                         strcpy(Result, (STRPTR)Answer->rm_Result2);
  173.                         DeleteArgstring((UBYTE *)Answer->rm_Result2);
  174.                     }
  175.                 }
  176.                 DeleteArgstring((UBYTE *)ARG0(Answer));
  177.                 DeleteRexxMsg(Answer);
  178.                 return Error;
  179.             }
  180.             else
  181.                 DeleteRexxMsg(rexxMsg);
  182.         }
  183.     }
  184.     Permit();
  185.     return RC_FATAL;
  186. }
  187.  
  188. int FindOldHeader(char * in, char * out, char * header)
  189. {
  190.     int n = 0, i = 0;
  191.     char * h;
  192.     BOOL end = FALSE;
  193.  
  194.     if(h = strstr(in, header))
  195.     {
  196.         while(h[n] != ':')
  197.             n++;
  198.         n += 2;
  199.         while(!end)
  200.         {
  201.             while(h[n] != 10)
  202.                 out[i++] = h[n++];
  203.             if(h[n-1] != ',')
  204.                 end = TRUE;
  205.             else
  206.             {
  207.                 while(isspace(h[n++])) ;
  208.                 n--;
  209.             }
  210.         }
  211.         out[i] = 0;
  212.         return(TRUE);
  213.     }
  214.     return(FALSE);
  215. }
  216.  
  217. void update_folder(void)
  218. {
  219.     struct MsgPort * ARexxPort;
  220.     char tmp[100];
  221.  
  222.     if(!(ARexxPort = CreateMsgPort()))
  223.         return;
  224.     SPrintf(buf, "SETFOLDER %ld", get_folder_pos(folders, DELETED));
  225.     SendRexxCommand("YAM", buf, ARexxPort, tmp);
  226.     SendRexxCommand("YAM", "MAILUPDATE", ARexxPort, tmp);
  227.     SPrintf(buf, "SETFOLDER %ld", get_folder_pos(folders, INCOMING));
  228.     SendRexxCommand("YAM", buf, ARexxPort, tmp);
  229.     DeleteMsgPort(ARexxPort);
  230. }
  231.  
  232. void _main(void)
  233. {
  234.     struct FileInfoBlock * fib = NULL;
  235.     BPTR lock = 0, fh, oldlock = 0;
  236.     static char * newsmsg = NULL, tmp[108], path[108];
  237.  
  238.     if(SysBase->LibNode.lib_Version < 39)
  239.         _exit(20);
  240.     if(query_for_user() == -1)
  241.         _exit(20);
  242.     if(!(folders = open_folder_list()))
  243.     {
  244.         if(folders = init_folder_list())
  245.         {
  246.             if(build_folders_list(folders) != -2)
  247.             {
  248.                 if(listpool = CreatePool(MEMF_ANY | MEMF_CLEAR, 8192, 4096))
  249.                 {
  250.                     if(LoadConfig())
  251.                     {
  252.                         save_folder_list(folders, newsgroupslist);
  253.                         free_folders_list(folders);
  254.                         if(listpool)
  255.                             DeletePool(listpool);
  256.                         folders = open_folder_list();
  257.                     }
  258.                     else
  259.                     {
  260.                         free_folders_list(folders);
  261.                         if(listpool)
  262.                             DeletePool(listpool);
  263.                         _exit(20);
  264.                     }
  265.                 }
  266.                 else
  267.                 {
  268.                     free_folders_list(folders);
  269.                     _exit(20);
  270.                 }
  271.             }
  272.             else
  273.             {
  274.                 free_folders_list(folders);
  275.                 _exit(20);
  276.             }
  277.         }
  278.         else
  279.             _exit(20);
  280.     }
  281.     strcpy(path, get_folder_path(folders, DELETED));
  282.     if(!strstr(path, ":"))
  283.     {
  284.         strcpy(path, current_user_path);
  285.         strcpy(tmp, get_folder_path(folders, DELETED));
  286.         AddPart(path, tmp, sizeof(path));
  287.     }
  288.     strcpy(buf, path);
  289.     AddPart(buf, ".fconfig", sizeof(buf));
  290.     if(!(lock = Lock(buf, ACCESS_READ)))
  291.     {
  292.         free_folders_list(folders);
  293.         _exit(20);
  294.     }
  295.     UnLock(lock);
  296.     if(!(listpool = CreatePool(MEMF_ANY | MEMF_CLEAR, 8192, 4096)))
  297.     {
  298.         free_folders_list(folders);
  299.         _exit(20);
  300.     }
  301.     if(!(LoadConfig()))
  302.     {
  303.         free_folders_list(folders);
  304.         if(listpool)
  305.             DeletePool(listpool);
  306.         _exit(20);
  307.     }
  308.     if(!(lock = Lock(path, ACCESS_READ)))
  309.     {
  310.         free_folders_list(folders);
  311.         if(listpool)
  312.             DeletePool(listpool);
  313.         _exit(20);
  314.     }
  315.     if(!(fib = AllocDosObject(DOS_FIB, TAG_DONE)))
  316.     {
  317.         free_folders_list(folders);
  318.         if(lock)
  319.             UnLock(lock);
  320.         if(listpool)
  321.             DeletePool(listpool);
  322.         _exit(20);
  323.     }
  324.     oldlock = CurrentDir(lock);
  325.     Examine(lock, fib);
  326.     while(ExNext(lock, fib))
  327.     {
  328.         if(fib->fib_EntryType < -2)
  329.         {
  330.             if(Stricmp(fib->fib_FileName, ".fconfig") && Stricmp(fib->fib_FileName, ".index"))
  331.             {
  332.                 static char header[60];
  333.  
  334.                 strcpy(tmp, fib->fib_FileName);
  335.                 if(fh = Open(tmp, MODE_OLDFILE))
  336.                 {
  337.                     if(newsmsg = (char *)AllocVec(fib->fib_Size + 2, MEMF_ANY | MEMF_CLEAR))
  338.                     {
  339.                         Read(fh, newsmsg, fib->fib_Size);
  340.                         if(FindOldHeader(newsmsg, tmp, "Newsgroups:"))
  341.                         {
  342.                             if(FindOldHeader(newsmsg, header, "Message-ID:"))
  343.                             {
  344.                                 BPTR fh2;
  345.                                 char name[108], * p, * nextname;
  346.  
  347.                                 for(nextname = strtok(tmp, ","); nextname; nextname = strtok(NULL, ","))
  348.                                 {
  349.                                     struct NewsNode * nn;
  350.                                     long cut = 0;
  351.  
  352.                                     strcpy(name, "YAM:NNTP-Headers/");
  353.                                     for(nn = (struct NewsNode *)newsgroupslist->lh_Head; nn->next; nn = (struct NewsNode *)nn->next)
  354.                                     {
  355.                                         if(Stricmp(nn->ngname, nextname) == 0)
  356.                                         {
  357.                                             cut = 1;
  358.                                             break;
  359.                                         }
  360.                                     }
  361.                                     if(cut)
  362.                                     {
  363.                                         strcat(name, nn->folder);
  364.                                         header[0] = 'p';
  365.                                         if(strlen(header) <= 30)
  366.                                         {
  367.                                             if(p = strchr(header, '>'))
  368.                                                 *p = 0;
  369.                                         }
  370.                                         else
  371.                                             header[30] = 0;
  372.                                         AddPart(name, header, sizeof(name));
  373.                                         if(fh2 = Lock(name, ACCESS_READ))
  374.                                         {
  375.                                             UnLock(fh2);
  376.                                             DeleteFile(name);
  377.                                         }
  378.                                         else
  379.                                         {
  380.                                             char name2[108];
  381.  
  382.                                             for(nn = (struct NewsNode *)newsgroupslist->lh_Head; nn->next; nn = (struct NewsNode *)nn->next)
  383.                                             {
  384.                                                 strcpy(name2, "YAM:NNTP-Headers/");
  385.                                                 strcat(name2, nn->folder);
  386.                                                 AddPart(name2, header, sizeof(name));
  387.                                                 if(fh2 = Lock(name2, ACCESS_READ))
  388.                                                 {
  389.                                                     UnLock(fh2);
  390.                                                     DeleteFile(name2);
  391.                                                     break;
  392.                                                 }
  393.                                             }
  394.                                         }
  395.                                         break;
  396.                                     }
  397.                                 }
  398.                             }
  399.                         }
  400.                         FreeVec(newsmsg);
  401.                     }
  402.                     Close(fh);
  403.                     DeleteFile(fib->fib_FileName);
  404.                 }
  405.             }
  406.         }
  407.     }
  408.     if(fib)
  409.         FreeDosObject(DOS_FIB, fib);
  410.     if(oldlock)
  411.         CurrentDir(oldlock);
  412.     if(lock)
  413.         UnLock(lock);
  414.     if(listpool)
  415.         DeletePool(listpool);
  416.     update_folder();
  417.     free_folders_list(folders);
  418.     _exit(0);
  419. }
  420.